/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import entities.Book;
import entities.Lending;
import entities.Node;
import entities.Reader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Chup Chep
 */
public class MyLinkList {

    Node head;
    Node tail;

    public boolean isEmpty() {
        return head == null;
    }

    public void addBookLast(Book book) {
        Node n = new Node(book);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.prev = tail;
            tail.next = n;
            tail = n;
        }
    }

    public void addReaderLast(Reader reader) {
        Node n = new Node(reader);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.prev = tail;
            tail.next = n;
            tail = n;
        }
    }

    public void addLendingLast(Lending lending) {
        Node n = new Node(lending);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.prev = tail;
            tail.next = n;
            tail = n;
        }
    }

    public void displayBook() {
        Node temp = head;
        double value;
        while (temp != null) {
            value = temp.info.getPrice()* temp.info.getQuantity();
            System.out.printf("%-11s %-17s %-16s %-10s %-11s %s", temp.info.getBcode(),
                    temp.info.getTitle(), temp.info.getQuantity(),
                    temp.info.getLended(), temp.info.getPrice(),value + "\n");
            temp = temp.next;
        }
    }

    public void addBookFirst(Book book) {
        Node n = new Node(book);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.next = head;
            head.prev = n;
            head = n;
        }
    }

    public void addReaderFirst(Reader reader) {
        Node n = new Node(reader);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.next = head;
            head.prev = n;
            head = n;
        }
    }

    public void addLendingFirst(Lending lending) {
        Node n = new Node(lending);
        if (isEmpty()) {
            head = tail = n;
        } else {
            n.next = head;
            head.prev = n;
            head = n;
        }
    }

    public Node bookSearch(String code) {
        Node temp = head;
        while (temp != null) {
            if (temp.info.getBcode().equals(code)) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    public Node readerSearch(String code) {
        Node temp = head;
        while (temp != null) {
            if (temp.reader.getRcode().equals(code)) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    public void deleteFirst() {
        if (!isEmpty()) {
            if (head == tail) {
                head = tail = null;
                return;
            } else {
                head = head.next;
                head.prev = null;
            }
        }
    }
     public void deleteLast() {
        if (!isEmpty()) {
            if (head == tail) {
                head = tail = null;
                return;
            } else {
                tail = tail.prev;
                tail.next = null;
            }
        }
    }

    public void bookDelete(String code) {
        Node temp = bookSearch(code);
        if (!isEmpty()) {
            if(temp==head){
                deleteFirst();
            }else if(temp==tail){
                deleteLast();
            } else {
                temp.prev.next = temp.next;
                temp.next.prev = temp.prev;
            }
        }
    }

    public void readerDelete(String code) {
        Node temp = bookSearch(code);
        if (!isEmpty()) {
            if(temp==head){
                deleteFirst();
            }else if(temp==tail){
                deleteLast();
            } else {
                temp.prev.next = temp.next;
                temp.next.prev = temp.prev;
            }
        }
    }

   

    public void insertAfterNodeBook(Book book, String code) {
        Node n = new Node(book);
        Node temp = bookSearch(code);
        if (temp != null) {
            if (temp == tail) {
                addBookLast(book);
                return;
            }
            temp.next.prev = n;
            n.next = temp.next;
            temp.next = n;
            n.prev = temp;
        } else {
            Node temp1 = head;
            if (isEmpty() || head == tail) {
                addBookLast(book);
                return;
            }
        }
    }

    public void insertAfterNodeReader(Reader reader) {
        Node n = new Node(reader);
        Node temp = readerSearch(reader.getRcode());
        if (temp != null) {
            if (temp == tail) {
                addReaderLast(reader);
                return;
            }
            temp.next.prev = n;
            n.next = temp.next;
            temp.next = n;
            n.prev = temp;
        } else {
            Node temp1 = head;
            if (isEmpty() || head == tail) {
                addReaderLast(reader);
                return;
            }
        }
    }

    public void sort() {
        Node i, j;
        i = head;
        Book temp;
        while (i != null) {
            j = i.next;
            while (j != null) {
                if (j.info.getBcode().compareToIgnoreCase(i.info.getBcode()) == 1) {
                    temp = i.info;
                    i.info = j.info;
                    j.info = temp;
                }
                j = j.next;
            }
            i = i.next;
        }
    }

     public boolean saveFile()
    {
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.dat"));
            out.writeObject(head);
            out.flush();
            out.close();
        } catch (Exception e) {
        }
        return  true;
    }
    
    public void readFile()
    {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.dat"));
            head =  (Node) ois.readObject();
            ois.close();
        } catch (Exception e) {
        }
    }
}
